home *** CD-ROM | disk | FTP | other *** search
- /**
- * Scout - The Amiga System Monitor
- *
- *------------------------------------------------------------------
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOse-> See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * You must not use this source code to gain profit of any kind!
- *
- *------------------------------------------------------------------
- *
- * @author Andreas Gelhausen
- * @author Richard Körber <rkoerber@gmx.de>
- */
-
- #include "system_headers.h"
-
- struct SemaphoresCallbackUserData {
- APTR ud_List;
- ULONG ud_Count;
- };
-
- static __asm __saveds LONG semalist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
- {
- return AllocListEntry(msg->pool, msg->entry, sizeof(struct SemaphoreEntry));
- }
-
- MakeHook(semalist_con2hook, semalist_con2func);
-
- static __asm __saveds LONG semalist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
- {
- FreeListEntry(msg->pool, &msg->entry);
-
- return 0;
- }
-
- MakeHook(semalist_des2hook, semalist_des2func);
-
- static __asm __saveds LONG semalist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
- {
- struct SemaphoreEntry *se = (struct SemaphoreEntry *)msg->entry;
-
- if (se) {
- msg->strings[0] = se->se_Address;
- msg->strings[1] = se->se_Name;
- msg->strings[2] = se->se_Pri;
- msg->strings[3] = se->se_NestCount;
- msg->strings[4] = se->se_QueueCount;
- msg->strings[5] = se->se_Owner;
- } else {
- msg->strings[0] = "Address";
- msg->strings[1] = "ln_Name";
- msg->strings[2] = "ln_Pri";
- msg->strings[3] = "ss_NestCount";
- msg->strings[4] = "ss_QueueCount";
- msg->strings[5] = "ss_Owner";
- msg->preparses[0] = MUIX_B;
- msg->preparses[1] = MUIX_B;
- msg->preparses[2] = MUIX_B;
- msg->preparses[3] = MUIX_B;
- msg->preparses[4] = MUIX_B;
- msg->preparses[5] = MUIX_B;
- }
-
- return 0;
- }
-
- MakeHook(semalist_dsp2hook, semalist_dsp2func);
-
- static LONG semalist_cmp2colfunc( struct SemaphoreEntry *se1,
- struct SemaphoreEntry *se2,
- ULONG column )
- {
- LONG pri1, pri2;
-
- switch (column) {
- case 0: return stricmp(se1->se_Address, se2->se_Address);
- case 1: return stricmp(se1->se_Name, se2->se_Name);
- case 2: IsDec(se1->se_Pri, &pri1); IsDec(se2->se_Pri, &pri2); return pri2 - pri1;
- case 3: return stricmp(se1->se_NestCount, se2->se_NestCount);
- case 4: return stricmp(se1->se_QueueCount, se2->se_QueueCount);
- case 5: return stricmp(se1->se_Owner, se2->se_Owner);
- }
- }
-
- static __asm __saveds LONG semalist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
- {
- LONG cmp;
- struct SemaphoreEntry *se1, *se2;
- ULONG col1, col2;
-
- se1 = (struct SemaphoreEntry *)msg->entry1;
- se2 = (struct SemaphoreEntry *)msg->entry2;
- col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
- col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
-
- if (msg->sort_type == MUIV_NList_SortType_None) return 0;
-
- if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
- cmp = semalist_cmp2colfunc(se2, se1, col1);
- } else {
- cmp = semalist_cmp2colfunc(se1, se2, col1);
- }
-
- if (cmp != 0 || col1 == col2) return cmp;
-
- if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
- cmp = semalist_cmp2colfunc(se2, se1, col2);
- } else {
- cmp = semalist_cmp2colfunc(se1, se2, col2);
- }
-
- return cmp;
- }
-
- MakeHook(semalist_cmp2hook, semalist_cmp2func);
-
- static void ReceiveList( void (* callback)( struct SemaphoreEntry *se, void *userData ),
- void *userData )
- {
- struct SemaphoreEntry *se;
-
- if (se = tbAllocVecPooled(globalPool, sizeof(struct SemaphoreEntry))) {
- if (SendDaemon("GetSemList")) {
- while (ReceiveDecodedEntry((UBYTE *)se, sizeof(struct SemaphoreEntry))) {
- callback(se, userData);
- }
- }
-
- tbFreeVecPooled(globalPool, se);
- }
- }
-
- static void IterateList( void (* callback)( struct SemaphoreEntry *se, void *userData ),
- void *userData )
- {
- struct SignalSemaphore *ss;
- struct MinList tmplist;
- struct SemaphoreEntry *se, *_se;
-
- NewList((struct List *)&tmplist);
-
- Forbid();
-
- ITERATE_LIST(&SysBase->SemaphoreList, struct SignalSemaphore *, ss) {
- if (se = AllocVec(sizeof(struct SemaphoreEntry), MEMF_PUBLIC)) {
- se->se_Addr = ss;
-
- _snprintf(se->se_Address, sizeof(se->se_Address), "$%08lx", ss);
- stccpy(se->se_Name, nonetest (ss->ss_Link.ln_Name), sizeof(se->se_Name));
- _snprintf(se->se_Pri, sizeof(se->se_Pri), "%4ld", ss->ss_Link.ln_Pri);
- _snprintf(se->se_NestCount, sizeof(se->se_NestCount), "%4ld", ss->ss_NestCount);
- _snprintf(se->se_QueueCount, sizeof(se->se_QueueCount), "%4ld", ss->ss_QueueCount);
- if (ss->ss_Owner) {
- GetTaskName(ss->ss_Owner, se->se_Owner, sizeof(se->se_Owner));
- } else {
- stccpy(se->se_Owner, "---", sizeof(se->se_Owner));
- }
-
- AddTail((struct List *)&tmplist, (struct Node *)se);
- }
- }
-
- Permit();
-
- ITERATE_CHANGING_LIST(&tmplist, struct SemaphoreEntry *, se, _se) {
- callback(se, userData);
- FreeVec(se);
- }
- }
-
- static void UpdateCallback( struct SemaphoreEntry *se,
- void *userData )
- {
- struct SemaphoresCallbackUserData *ud = (struct SemaphoresCallbackUserData *)userData;
-
- InsertSortedEntry(ud->ud_List, se);
- ud->ud_Count++;
- }
-
- static void PrintCallback( struct SemaphoreEntry *se,
- void *userData )
- {
- PrintFOneLine((BPTR)userData, " %s %-30s %4s %4s %5s %s\n", se->se_Address, se->se_Name, se->se_Pri, se->se_NestCount, se->se_QueueCount, se->se_Owner);
- }
-
- static void SendCallback( struct SemaphoreEntry *se,
- void *userData )
- {
- SendEncodedEntry((UBYTE *)se, sizeof(struct SemaphoreEntry));
- }
-
- static ULONG __saveds mNew( struct IClass *cl,
- Object *obj,
- struct opSet *msg )
- {
- APTR semlist, semtext, semcount, updateButton, printButton, obtainButton, releaseButton, removeButton, priorityButton, exitButton;
-
- if (obj = (Object *)DoSuperNew(cl, obj,
- MUIA_HelpNode, SemaphoresText,
- MUIA_Window_ID, MakeID('S','E','M','A'),
- WindowContents, VGroup,
-
- Child, semlist = MyNListviewObject(MakeID('S','E','L','V'), "BAR,BAR,BAR P=" MUIX_R ",BAR P=" MUIX_C ",BAR P=" MUIX_C ",BAR", &semalist_con2hook, &semalist_des2hook, &semalist_dsp2hook, &semalist_cmp2hook, TRUE),
- Child, MyBelowListview(&semtext, &semcount),
-
- Child, MyVSpace(4),
-
- Child, HGroup, MUIA_Group_SameSize, TRUE,
- Child, updateButton = MakeButton(txtUpdate),
- Child, printButton = MakeButton(txtPrint),
- Child, obtainButton = MakeButton(txtObtain),
- Child, releaseButton = MakeButton(txtRelease),
- Child, removeButton = MakeButton(txtRemove),
- Child, priorityButton = MakeButton(txtPriority),
- Child, exitButton = MakeButton(txtExit),
- End,
- End,
- TAG_MORE, msg->ops_AttrList))
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- APTR parent;
-
- swd->swd_SemaphoreList = semlist;
- swd->swd_SemaphoreText = semtext;
- swd->swd_SemaphoreCount = semcount;
- swd->swd_ObtainButton = obtainButton;
- swd->swd_ReleaseButton = releaseButton;
- swd->swd_RemoveButton = removeButton;
- swd->swd_PriorityButton = priorityButton;
-
- parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
-
- set(obj, MUIA_Window_Title, MyGetWindowTitle("SEMAPHORES", swd->swd_Title, sizeof(swd->swd_Title)));
- set(obj, MUIA_Window_ActiveObject, semlist);
- set(obtainButton, MUIA_Disabled, TRUE);
- set(releaseButton, MUIA_Disabled, TRUE);
- set(removeButton, MUIA_Disabled, TRUE);
- set(priorityButton, MUIA_Disabled, TRUE);
-
- DoMethod(parent, MUIM_Window_AddChildWindow, obj);
- DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
- DoMethod(semlist, MUIM_Notify, MUIA_NList_Active, MUIV_EveryTime, obj, 1, MUIM_SemaphoresWin_ListChange);
- DoMethod(updateButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Update);
- DoMethod(printButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Print);
- DoMethod(obtainButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Obtain);
- DoMethod(releaseButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Release);
- DoMethod(removeButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Remove);
- DoMethod(priorityButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, MUIM_SemaphoresWin_Priority);
- DoMethod(exitButton, MUIM_Notify, MUIA_Pressed, FALSE, obj, 3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
- DoMethod(semlist, MUIM_NList_Sort3, MUIV_NList_Sort3_SortType_1, MUIV_NList_SortTypeAdd_None, MUIV_NList_Sort3_SortType_Both);
- }
-
- return (ULONG)obj;
- }
-
- static ULONG __saveds mDispose( struct IClass *cl,
- Object *obj,
- struct opSet *msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
-
- set(obj, MUIA_Window_Open, FALSE);
- DoMethod(swd->swd_SemaphoreList, MUIM_NList_Clear);
-
- return (DoSuperMethodA(cl, obj, msg));
- }
-
- static ULONG __saveds mUpdate( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoresCallbackUserData ud;
-
- ApplicationSleep(TRUE);
- set(swd->swd_SemaphoreList, MUIA_NList_Quiet, TRUE);
- DoMethod(swd->swd_SemaphoreList, MUIM_NList_Clear);
-
- ud.ud_List = swd->swd_SemaphoreList;
- ud.ud_Count = 0;
-
- if (clientstate) {
- ReceiveList(UpdateCallback, &ud);
- } else {
- IterateList(UpdateCallback, &ud);
- }
-
- SetCountText(swd->swd_SemaphoreCount, ud.ud_Count);
- MySetContents(swd->swd_SemaphoreText, "");
-
- set(swd->swd_SemaphoreList, MUIA_NList_Quiet, FALSE);
- set(swd->swd_ObtainButton, MUIA_Disabled, TRUE);
- set(swd->swd_ReleaseButton, MUIA_Disabled, TRUE);
- set(swd->swd_RemoveButton, MUIA_Disabled, TRUE);
- set(swd->swd_PriorityButton, MUIA_Disabled, TRUE);
- ApplicationSleep(FALSE);
-
- return 0;
- }
-
- static ULONG __saveds mPrint( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- PrintSemaphores(NULL);
-
- return 0;
- }
-
- static ULONG __saveds mObtain( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoreEntry *se;
-
- if (se = (struct SemaphoreEntry *)GetActiveEntry(swd->swd_SemaphoreList)) {
- if (MyRequest(msgYesNo, msgWantToObtainSemaphore, se->se_Name)) {
- MyDoCommand("ObtainSemaphore %s", se->se_Address);
- DoMethod(obj, MUIM_SemaphoresWin_Update);
- }
- }
-
- return 0;
- }
-
- static ULONG __saveds mRelease( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoreEntry *se;
-
- if (se = (struct SemaphoreEntry *)GetActiveEntry(swd->swd_SemaphoreList)) {
- LONG nest;
-
- if (IsDec(se->se_NestCount, &nest) && nest > 0) {
- if (MyRequest(msgYesNo, msgWantToReleaseSemaphore, se->se_Name)) {
- MyDoCommand("ReleaseSemaphore %s", se->se_Address);
- DoMethod(obj, MUIM_SemaphoresWin_Update);
- }
- } else {
- MyRequest(msgErrorContinue, msgNestCountIsZero);
- }
- }
-
- return 0;
- }
-
- static ULONG __saveds mRemove( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoreEntry *se;
-
- if (se = (struct SemaphoreEntry *)GetActiveEntry(swd->swd_SemaphoreList)) {
- if (MyRequest(msgYesNo, msgWantToRemoveSemaphore, se->se_Name)) {
- MyDoCommand("RemoveSemaphore %s", se->se_Address);
- DoMethod(obj, MUIM_SemaphoresWin_Update);
- }
- }
-
- return 0;
- }
-
- static ULONG __saveds mPriority( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoreEntry *se;
-
- if (se = (struct SemaphoreEntry *)GetActiveEntry(swd->swd_SemaphoreList)) {
- LONG pri;
-
- pri = atol(se->se_Pri);
- if (GetPriority(se->se_Name, &pri)) {
- if (MyDoCommand("SetPriority SEMAPHORE \"%s\" %ld", se->se_Name, pri)) {
- _snprintf(se->se_Pri, sizeof(se->se_Pri), "%4ld", pri);
- RedrawActiveEntry(swd->swd_SemaphoreList);
- }
- }
- }
-
- return 0;
- }
-
- static ULONG __saveds mListChange( struct IClass *cl,
- Object *obj,
- Msg msg )
- {
- struct SemaphoresWinData *swd = INST_DATA(cl, obj);
- struct SemaphoreEntry *se;
-
- if (se = (struct SemaphoreEntry *)GetActiveEntry(swd->swd_SemaphoreList)) {
- MySetContents(swd->swd_SemaphoreText, "%s \"%s\"", se->se_Address, se->se_Name);
- set(swd->swd_ObtainButton, MUIA_Disabled, FALSE);
- set(swd->swd_ReleaseButton, MUIA_Disabled, FALSE);
- set(swd->swd_RemoveButton, MUIA_Disabled, FALSE);
- set(swd->swd_PriorityButton, MUIA_Disabled, FALSE);
- }
-
- return 0;
- }
-
- ULONG __asm __saveds SemaphoresWinDispatcher( register __a0 struct IClass *cl,
- register __a2 Object *obj,
- register __a1 Msg msg )
- {
- switch (msg->MethodID) {
- case OM_NEW: return (mNew(cl, obj, (APTR)msg));
- case OM_DISPOSE: return (mDispose(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Update: return (mUpdate(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Print: return (mPrint(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Obtain: return (mObtain(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Release: return (mRelease(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Remove: return (mRemove(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_Priority: return (mPriority(cl, obj, (APTR)msg));
- case MUIM_SemaphoresWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
- }
-
- return (DoSuperMethodA(cl, obj, msg));
- }
-
- void PrintSemaphores( char *filename )
- {
- BPTR handle;
-
- if (handle = HandlePrintStart(filename)) {
- PrintFOneLine(handle, "\n Address Name Pri Nest Queue Owner\n\n");
- IterateList(PrintCallback, (void *)handle);
- }
-
- HandlePrintStop();
- }
-
- void SendSemList( void )
- {
- IterateList(SendCallback, NULL);
- }
-
-